package housekeeping.controller;

import housekeeping.pojo.Worker;
import housekeeping.service.WorkerService;
import housekeeping.util.RedisUtil;
import housekeeping.util.ServerResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: WorkerController
 * Description:
 *
 * @Author 迷藏
 * @Create 2025/7/7 21:47
 * @Version 1.0
 */
@CrossOrigin
@RestController
@Tag(name = "家政人员管理")
public class WorkerController {
    @Autowired
    private WorkerService workerService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/worker/selectByCategoryId")
    @Operation(summary = "根据服务类型查询家政人员")
    private ServerResult<List<Worker>> selectByCategoryId(Integer id){
        List<Worker> workers = workerService.selectByCategoryId(id);
        return ServerResult.ok(workers);
    }

    @GetMapping("/worker/selectByServiceType")
    @Operation(summary = "根据服务类型查询家政人员")
    private ServerResult<List<Worker>> selectByServiceType(@Param("serviceType")String serviceType){
        List<Worker> workers = workerService.selectByServiceType(serviceType);
        return ServerResult.ok(workers);
    }

    @GetMapping("/worker/selectById")
    @Operation(summary = "根据id查询家政人员")
    private ServerResult<Worker> selectById(Integer id){
        Worker worker = workerService.selectById(id);
        return ServerResult.ok(worker);
    }

    @GetMapping("/worker/list")
    @Operation(summary = "显示所有家政人员")
    public ServerResult<List<Worker>> getWorkers(){
        List<Worker> list = workerService.getWorkers();
        return ServerResult.ok(list);
    }

    @DeleteMapping("/worker/delete/{id}")
    @Operation(summary = "删除家政人员")
    public ServerResult<Void> deleteWorker(@PathVariable Integer id){
        workerService.deleteWorker(id);
        return ServerResult.ok();
    }

    @PostMapping("/worker/update")
    @Operation(summary = "更新家政人员信息")
    public ServerResult<Void> updateWorker(@RequestBody Worker worker){
        try{
            workerService.updateWorker(worker);
        }
        catch (RuntimeException e){
            return ServerResult.error(500, e.getMessage());
        }
        return ServerResult.ok();
    }

    @GetMapping("/worker/redis")
    @Operation(summary = "测试redis,根据redis中的id查询家政人员信息")
    public ServerResult<Worker> redis(){
        String key = "user";
        String account = redisUtil.getUserAccount(key);
        String role = redisUtil.getUserRole(key);
        Worker worker = workerService.selectByAccount(account);
        return ServerResult.ok(worker);
    }
}
